<?php
/**
 * @package PDO_For_Wordpress
 * @version $Id$
 * @author	Justin Adie, rathercurious.net
 */

 /**
  *	this script just installs the base database schema as part of the installation process
  */

function installDB(){
	require_once PDODIR . 'driver_sqlite/pdo_sqlite_driver_create.php';
	ob_end_clean();

	require_once(ABSPATH . 'wp-admin/includes/schema.php');
	require PDODIR . '/dialect/wp_sqlite_schema.php';
	// Populate for back compat.
	echo "installing the database <br/>";
	$wp_queries = wp_get_db_sqlite_schema( 'all' );
	if (defined('PDO_DEBUG') && PDO_DEBUG === true){
		$today = date("Ymd");
		file_put_contents(ABSPATH."wp-content/database/debug$today.log", $wp_queries."\n", FILE_APPEND);
		echo "sql log of schema in " . ABSPATH . "wp-content/database/debug$today.log<br/>";
	}

	//add a query to deal with file mods
	$query = "create table modTimes (modFile text not null primary key, modTime text not null default '0000-00-00 00:00:00')";

	$queries = explode (";", $wp_queries);
	array_push($queries, $query);

	$pdo = new PDO('sqlite:'.FQDB);
	foreach ($queries as $query){
		$query = trim($query);
		if (empty($query)) continue;
		$q = new createQuery();
		$_q = $q->rewriteQuery($query);
		echo substr($_q, 0,120) . " ...<br/>";
		$q = NULL;
		if (is_array($_q)){
			foreach ($_q as $__q){
				if (!empty($__q)){
					$result = $pdo->exec($__q);
					if ($result === false){
						echo "Error installing the database<br/>Query was $__q.<br/>Error message was: " . print_r($wpdb->pdo->errorInfo(), true);
					}
				}
			}
		} else {
			$result = $pdo->exec($_q);
			if ($result === false){
				echo "Error installing the database<br/>Query was $_q.<br/>Error message was: ";
				echo "Error installing the database<br/>Query was $_q.<br/>Error message was: " . print_r($wpdb->errorInfo(), true);
			}
		}
	}
	return true;
}
?>